package com.offcn.util;


import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BaseDao<T> {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 通用增删改
     * @param sql           sql语句对象
     * @param params        参数对象
     */
    public int update(String sql,Object ...params){
        Connection connection = JdbcUtils.getConnection();
        try {
            return queryRunner.update(connection,sql,params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        } finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    public int update(String sql,Object entity,String field){
        Connection connection = JdbcUtils.getConnection();
        try {
            List params = new ArrayList();
            String[] fields = field.split(",");
            for(int i = 0 ; i < fields.length ; i++){
                Field declaredField = entity.getClass().getDeclaredField(fields[i]);
                declaredField.setAccessible(true);
                Object p = declaredField.get(entity);
                params.add(p);
            }
            return queryRunner.update(connection,sql,params.toArray());
        } catch (SQLException | NoSuchFieldException | IllegalAccessException throwables) {
            throwables.printStackTrace();
            return 0;
        } finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    //查询单条记录，返回一个实体类 javaBean
    public T queryForSingle(String sql,Class clazz,Object...params){
        Connection connection = JdbcUtils.getConnection();
        try {
            T t = queryRunner.query(connection, sql, new BeanHandler<T>(clazz), params);
            return t;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        } finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    // 查询多条记录， 返回一个集合，集合的泛型是一个实体类，javaBean
    public List<T> queryForList( String sql, Class clazz, Object...params){
        Connection connection = JdbcUtils.getConnection();
        try {
            List<T> list = queryRunner.query(connection,sql, new BeanListHandler<T>(clazz), params);
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        } finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
